class code1 {
    //螺旋矩阵:找规律
    public List<Integer> spiralOrder(int[][] matrix) {
        //特判
        if(matrix.length == 0) return new ArrayList<Integer>();

        int left = 0;
        int right = matrix[0].length-1;
        int top = 0;
        int bottom = matrix.length-1;

        List<Integer> res = new ArrayList<>();

        while(true) {
            for(int j=left;j<=right;j++) res.add(matrix[top][j]);// left to right
            if(++top>bottom) break;
            for(int j=top;j<=bottom;j++) res.add(matrix[j][right]);// top to bottom
            if(left>--right) break;
            for(int i=right;i>=left;i--) res.add(matrix[bottom][i]);// right to left
            if(top>--bottom) break;
            for(int i=bottom;i>=top;i--) res.add(matrix[i][left]);// bottom to top
            if(++left>right) break;
        }
        return res;
    }
}